package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.GoodsCategory;
import com.ruoyi.system.mapper.GoodsCategoryMapper;
import com.ruoyi.system.service.IGoodsCategoryService;
import io.jsonwebtoken.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-21
 */
@Slf4j
@Service
public class GoodsCategoryServiceImpl implements IGoodsCategoryService {
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public GoodsCategory selectGoodsCategoryById(String id) {
        return goodsCategoryMapper.selectGoodsCategoryById(id);
    }

    /**
     * 查询商品分类列表
     *
     * @param goodsCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.selectGoodsCategoryList(goodsCategory);
    }

    /**
     * 新增商品分类
     *
     * @param goodsCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertGoodsCategory(GoodsCategory goodsCategory) {
        goodsCategory.setCreateTime(DateUtils.getNowDate());
        goodsCategory.setUpdateTime(DateUtils.getNowDate());
        goodsCategory.setCreateBy(SecurityUtils.getUserId() + "");
        return goodsCategoryMapper.insertGoodsCategory(goodsCategory);
    }

    /**
     * 修改商品分类
     *
     * @param goodsCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateGoodsCategory(GoodsCategory goodsCategory) {
        goodsCategory.setUpdateTime(DateUtils.getNowDate());
        goodsCategory.setUpdateBy(SecurityUtils.getUserId() + "");
        return goodsCategoryMapper.updateGoodsCategory(goodsCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryByIds(String[] ids) {
        Integer goodsCount = goodsCategoryMapper.selectGoodsCategoryByIds(ids);
        Assert.isTrue(goodsCount <= 0, "存在关联商品数据，不允许删除");
        return goodsCategoryMapper.deleteGoodsCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryById(String id) {
        return goodsCategoryMapper.deleteGoodsCategoryById(id);
    }
}
